package com.rw.tool.test.exception.extra;

import com.rw.tool.util.expression.element.Calculation;
import com.rw.tool.util.expression.exception.SignException;
import com.rw.tool.util.expression.expression.calculator.Calculator;
import com.rw.tool.util.expression.expression.calculator.element.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class DiceCalculator extends Calculator {
    /**
     * 计算一对数字
     */
    @Override
    protected BigDecimal calculationOne(BigDecimal leftNumber, Calculation calculation, BigDecimal rightNumber) {
        List<CalculatorElement> calculatorElementList = getCalculatorElementList();

        for (CalculatorElement calculatorElement : calculatorElementList) {
            if (calculatorElement.trigger(calculation)) {
                return calculatorElement.handler(leftNumber, calculation, rightNumber);
            }
        }

        throw new SignException(calculation.toString());
    }

    /**
     * 计算一对数字
     */
    @Override
    protected List<CalculatorElement> getCalculatorElementList() {
        List<CalculatorElement> calculatorElementList = new ArrayList<>();

        calculatorElementList.add(new PlusCalculatorElement());
        calculatorElementList.add(new MinusCalculatorElement());
        calculatorElementList.add(new DivisionCalculatorElement());
        calculatorElementList.add(new MultiCalculatorElement());
        calculatorElementList.add(new DiceCalculatorElement());

        return calculatorElementList;
    }
}
